﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;

namespace SksApp
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (File.Exists(Application.StartupPath + "\\SksApp.exe.config"))
            {
                string data = File.ReadAllText(Application.StartupPath + "\\SksApp.exe.config");
                int bas = data.IndexOf("Data Source=");
                int son = data.IndexOf("SKS.sdf");
                string rep = data.Substring(bas + 12, son - bas - 12);
                data = data.Replace(rep, Application.StartupPath + "\\");
                File.WriteAllText(Application.StartupPath + "\\SksApp.exe.config", data);
            }

            using (SksEntity ent = new SksEntity())
            {
                if (ent.Database.CreateIfNotExists())
                {
                    ent.Database.CreateUniqueIndex<Magaza>(x => x.No);
                    ent.Database.CreateIndex<Magaza>(x => x.Adi);

                    Magaza mg = new Magaza();
                    mg.Adi = "Merkez";
                    mg.FisLimiti = 600;
                    mg.No = 1;
                    mg.OnaySekli = 0;
                    ent.Magazalar.Add(mg);
                    ent.SaveChanges();
                    Kasiyer ks = new Kasiyer();
                    ks.MagazaID = mg.ID;
                    ks.No = 1;
                    ks.SablonID = -1;
                    ks.Sifresi = "1";
                    ks.Adi = "Admin";
                    ent.Kasiyerler.Add(ks);

                    int[] vergiler = new int[4] { 0, 1, 8, 18 };
                    for (int i = 0; i < 4; i++)
                    {
                        VergiTanim tnm = new VergiTanim();
                        tnm.Aciklama = "% " + vergiler[i].ToString() + " Kdv";
                        tnm.No = (byte)(i + 1);
                        tnm.Oran = vergiler[i];
                        tnm.Sira = i + 1;
                        ent.VergiOranlari.Add(tnm);
                    }

                    ent.SaveChanges();
                }
            }

            Settings.Instance.Path = Application.StartupPath;
            Settings.Instance.Load();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainView());

            Settings.Instance.Save();
        }
    }
}
